// 获取应用实例
const app = getApp()
import {
    util,
    Toast,
    Dialog,
    commonMixin
} from "../../utils/util.js";

Page({

  behaviors: [commonMixin],
    /**
     * 页面的初始数据
     */
    data: {
        detail: {
            banners: [],
        },
        products: [],
        saveImageShow: false, //保存海报的弹框
        saveImageUrl: '', //保存的海报
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        swiperIndex: 0,
        longitude: 121.477091,
        latitude: 31.232718,
        locations: [],
        showLocation: false,
        showLocationType: false,
        banners: [
            'https://img01.yzcdn.cn/vant/cat.jpeg',
            'https://img01.yzcdn.cn/vant/cat.jpeg',
            'https://img01.yzcdn.cn/vant/cat.jpeg'
        ], //banner
        showPopup: false, //是否锁
        id: 0, //商品ID
    },

    onShow() {

    },
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: options.id
            })
        }
        this.getInfoData();
    },

    onLocationTypeClose() {
      this.setData({
        showLocationType: false
      });
    },

    //立即购买
    buyGoodsNow() {
      this.setData({
        showLocationType: true
      });
    },

    handleBuyGood() {
      let userInfo = util.getUserInfo()
      if (!userInfo.status) {
        app.globalData.loginFlag = true;
        this.navToUrl('/pages/my/index');
      } else {
        this.generateOrder(0);
      }
    },

    handleBuyServiceGood() {
      let userInfo = util.getUserInfo()
      if (!userInfo.status) {
        app.globalData.loginFlag = true;
        this.navToUrl('/pages/my/index');
      } else {
        this.generateOrder(1);
      }
    },

    generateOrder(locationType) {
      wx.showLoading({
        title: '加载中',
      })
      util.http.post("v1/mini/generate-order",{
        check_item_id: this.data.id,
        location_type: locationType
      }).then((res) => {
          if (res.data.code == 0) {
            wx.navigateTo({
                url: '/pages/shopCart/index?id=' + res.data.data.id
            })
          } else {
            Toast(res.data.message);
          }
          wx.hideLoading();
      }).catch((err) => {
          wx.hideLoading();
          Toast('网络异常！刷新后重试！');
          console.log(err)
      })
    },

    handleOpenLocation() {
      this.setData({
        showLocation: true
      })
    },

    onLocationClose() {
      this.setData({
        showLocation: false
      })
    },

    //获取详情数据
    getInfoData() {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        // let userInfo = util.getUserInfo();
        // let openid = userInfo.openid;

        var latitude = this.data.latitude;
        var longitude = this.data.longitude;
        wx.getLocation({
            type: 'wgs84',
            success: (res) => {
              latitude = res.latitude; // 纬度
              longitude = res.longitude; // 经度
              util.http.post("v1/mini/get-locations", {
                latitude: latitude,
                longitude: longitude
              }).then((res) => {
                res.data.data.list.sort((a, b) => a.distance - b.distance);
                this.setData({
                  locations: res.data.data.list
                })
              }).catch((err) => {
              })
            },
            fail: (err) => {
              util.http.post("v1/mini/get-locations", {
                latitude: latitude,
                longitude: longitude
              }).then((res) => {
                res.data.data.list.sort((a, b) => a.distance - b.distance);
                this.setData({
                  locations: res.data.data.list
                })
              }).catch((err) => {
              })
            }
        });

        util.http.post("v1/mini/check-item-get-detail", {
            id: this.data.id
        }).then((res) => {
            if (res.data.code == 0) {
                res.data.data.content = res.data.data.content.replace(/<img([^>]*)>/gi, '<img style="max-width: 100%; display: block; margin: auto;" $1>');
                this.setData({
                    detail: res.data.data
                })
            } else {
                Toast(res.data.message);
            }
            wx.hideLoading();
        }).catch((err) => {
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            console.log(err)
        })
        // util.http.post("/api/product/info", {
        //     openid: openid,
        //     id: this.data.id,
        // }).then((res) => {
        //     if (res.data.code == 0) {

        //         this.setData({
        //             type: res.data.data.type, //类型
        //             banners: res.data.data.c_images,
        //             title: res.data.data.title, //标题
        //             sub_title: res.data.data.sub_title, //副标题
        //             vip_price: res.data.data.vip_price, //vip价格
        //             price: res.data.data.price, //价格
        //             specifications: res.data.data.specifications, //规格图片
        //             serviceChat: res.data.data.serviceChat, //客服
        //             s_image: res.data.data.s_image,
        //             ewm: res.data.data.ewm,
        //             is_collection: res.data.data.is_collection, //收藏
        //         })


        //         //产品详情
        //         let article = res.data.data.info_content ? res.data.data.info_content : "<div style='text-align:center'>暂无数据...</div>";
        //         WxParse.wxParse('article', 'html', article, this, 25);

        //     } else {
        //         Toast(res.data.message);
        //     }
        //     wx.hideLoading();
        // }).catch((err) => {
        //     wx.hideLoading();
        //     Toast('网络异常！刷新后重试！');
        //     console.log(err)
        // })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {


    },

    //swiper g改变
    swiperChange(e) {
        this.setData({
            swiperIndex: e.detail.current
        })
    },
})
